<template>
  <el-dialog
    title="冰拓"
    destroy-on-close
    :visible.sync="dialogVisible"
    width="30%"
    @close="close"
  >
    <el-form
      ref="onok"
      label-width="80px"

      :model="form"
      :rules="rules"
    >
      <el-form-item label="账号：" prop="username">
        <el-input v-model="form.username" />
      </el-form-item>
      <el-form-item label="密码：" prop="passwd">
        <el-input v-model="form.passwd" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="loading" @click="dialogVisible = false">取 消</el-button>
      <el-button
        :loading="loading"
        type="primary"
        @click="onSubmit('onok')"
      >确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { handelIceExtend } from '../../../request/facilityList'
import { USERPASS } from './constants'
export default {
  data() {
    return {
      dialogVisible: false,
      loading: false,
      form: {
        username: '',
        passwd: ''
      },
      rules: USERPASS
    }
  },
  created() {
    this.handelIceExtend('get')
  },
  methods: {
    close() {
      this.loading = false
    },
    onSubmit(name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          this.loading = true
          this.handelIceExtend('update', 'post')
          this.handelClose()
          this.loading = false
        }
        // else {
        //   this.$error(res.msg)
        // }
      })
    },
    async handelIceExtend(type, method) {
      const res = await handelIceExtend(type, this.form, method)
      if (!method && res) {
        this.$set(this.form, 'username', res.username)
        this.$set(this.form, 'passwd', res.passwd)
        return
      }
      if (res) {
        this.$success('操作成功')
      }
    },
    handelOpen() {
      this.dialogVisible = true
    },
    handelClose() {
      this.dialogVisible = false
    }
  }
}
</script>
<style scoped></style>
